<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav"
      left-arrow
      @click-left="$router.back()"
      title="登录"
    />
    <!-- /导航栏 -->

    <!-- form表单 -->
    <van-form @submit="onSubmit" ref="formRef">
      <van-field
        v-model="user.mobile"
        type="number"
        name="moblie"
        maxlength="11"
        placeholder="请输入手机号"
        :rules="userRules.moblie"
        class="center"
      >
        <template slot="left-icon">
          <i class="toutiao toutiao-shouji icon"></i>
        </template>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        type="number"
        maxlength="6"
        placeholder="万能验证码246810"
        :rules="userRules.code"
        class="center"
      >
        <template slot="left-icon">
          <i class="toutiao toutiao-yanzhengma icon"></i>
        </template>

        <template #button>
          <van-count-down
            v-if="isDaoJiShi"
            :time="1000 * 60"
            format="ss s"
            @finish="isDaoJiShi = false"
          />

          <van-button
            v-else
            size="small"
            class="yanzheng"
            type="default"
            round
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button class="submit" block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
    <!-- /form表单 -->
  </div>
</template>

<script>
import { login, sendSms } from "@/api/user.js";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      user: {
        mobile: "18336110011",
        code: "246810",
      },
      // 表单验证
      userRules: {
        moblie: [
          {
            require: true,
            message: "请输入手机号",
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
          },
        ],
        code: [
          {
            require: true,
            message: "请输入正确验证码",
          },
          {
            pattern: /\d{6}/,
            message: "验证码格式不正确",
          },
        ],
      },
      // 显示倒计时和按钮
      isDaoJiShi: false,
    };
  },
  methods: {
    // vuex中的方法
    ...mapMutations(["setUser"]),

    //   登录事件
    async onSubmit() {
      // 加载
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });
      //
      try {
        const { data: res } = await login(this.user);
        this.$toast.success("登录成功");
        this.setUser(res.data);
        this.$router.push("/my");
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail("手机号或者验证码错误");
        } else {
          console.log(err);
          this.$toast.fail("登录失败，稍后重试");
        }
      }

      // 4 根据响应后续操作
    },
    // 点击发送验证码
    async onSendSms() {
      // 1 验证手机号
      try {
        await this.$refs.formRef.validate("moblie");
      } catch (err) {
        return this.$toast.fail(err.message);
      }
      // 2 显示倒计时

      this.isDaoJiShi = true;
      // 3 请求发送验证码

      try {
        await sendSms(this, user.mobile);
        console.log("发送成功");
      } catch (err) {
        console.log(err);
      }
    },
  },
  created() {},
};
</script>

<style lang="less" scoped>
.icon {
  font-size: 37px;
}
.yanzheng {
  background-color: rgb(238, 238, 238);
  font-size: 22px;
  color: #666;
}
.submit {
  background-color: rgb(102, 177, 255);
  border: none;
}
.center {
  display: flex;
  align-items: center;
}
</style>